<?php

/**
 * SectionController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_SectionController extends Zend_Controller_Action {
	
	/**
	 * @see Zend_Controller_Action::init()
	 *
	 */
	public function init() {
		$this->view->baseUrl = $this->_request->getBaseURL ();
		$this->view->contextPath = "administrator/section";
		$this->view->pageTitle = "eway.vn: Section Management";
	}
	
	/**
	 * @see Zend_Controller_Action::preDispatch()
	 *
	 */
	public function preDispatch() {
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$itemPerPage = 30;
		
		// Get list of existing sections for displaying
		$section = new SectionModel ( );
		$totalSections = $section->fetchAll ()->count ();
		
		$currentPage = ( int ) $this->_request->getParam ( "page" );
		$totalPages = floor ( ($totalSections / $itemPerPage) + ((($totalSections % $itemPerPage) == 0) ? 0 : 1) );
		
		if (($currentPage < 1) || ($currentPage > $totalPages)) {
			$currentPage = 1;
		}
		
		$this->view->count = $totalSections;
		$this->view->pages = $totalPages;
		$this->view->sections = $section->fetchAll ( null, "sec_position ASC", $itemPerPage, ($currentPage - 1) * $itemPerPage );
	}
	
	/**
	 * Add a new section
	 *
	 */
	public function addAction() {
		
		$section = new SectionModel ( );
		
		// Process section adding
		if ($this->_request->isPost ()) {
			
			$filter = new Zend_Filter_StripTags ( );
			
			$name = $filter->filter ( $this->_request->getPost ( "name" ) );
			$description = $filter->filter ( $this->_request->getPost ( "description" ) );
			$price = ( int ) $this->_request->getPost ( "price" );
			
			if (($name != '') && ($price > 0)) {
				$position = $section->fetchAll ()->count () + 1;
				$data = array ("sec_name" => $name, "sec_description" => $description, "sec_position" => $position, "sec_price" => $price );
				
				$section->insert ( $data );
				
				$this->_redirect ( "administrator/section" );
				return;
			}
		
		}
		
		// Create an empty row
		$this->view->section = $section->createRow ();
		
		// Form attributes
		$this->view->action = "add";
		$this->view->buttonText = "Add new";
	}
	
	/**
	 * Edit an existing section
	 *
	 */
	public function editAction() {
		
		$section = new SectionModel ( );
		
		// Process section update
		if ($this->_request->isPost ()) {
			
			$filter = new Zend_Filter_StripTags ( );
			
			$id = ( int ) $this->_request->getPost ( "id" );
			
			if ($id < 0) {
				$this->_redirect ( "administrator/section" );
				return;
			}
			
			$name = $filter->filter ( $this->_request->getPost ( "name" ) );
			$description = $filter->filter ( $this->_request->getPost ( "description" ) );
			$price = ( int ) $this->_request->getPost ( "price" );
			
			if (($name != '') && ($price > 0)) {
				$data = array ("sec_id" => $id, "sec_name" => $name, "sec_description" => $description, "sec_price" => $price );
				
				$section->update ( $data, "sec_id=" . $id );
			}
			
			$this->_redirect ( "administrator/section" );
			return;
		
		} else {
			
			// Find the selected section to update
			$id = ( int ) $this->_request->getParam ( "id" );
			$this->view->section = $section->fetchRow ( "sec_id=" . $id );
			
			// Form attributes
			$this->view->action = "edit";
			$this->view->buttonText = "Update";
		}
	}
	
	// Delete a section
	public function deleteAction() {
		// Find the selected section to update
		$id = ( int ) $this->_request->getParam ( "id" );
		
		if ($id <= 0) {
			return;
		}
		
		$section = new SectionModel ( );
		$section->delete ( "sec_id=" . $id );
		
		$this->_redirect ( "administrator/section" );
	}
	
	// Update the position
	public function positionAction() {
		$id = ( int ) $this->_request->getParam ( "id" );
		
		if ($id <= 0) {
			return;
		}
		
		$position = ( int ) $this->_request->getParam ( "val" );
		
		if ($position <= 0) {
			$position = 1;
		}
		
		$data = array ("sec_position" => $position );
		
		$section = new SectionModel ( );
		$section->update ( $data, "sec_id=" . $id );
		
		$this->_redirect ( "administrator/section" );
	}

}
